package com.wty.shop.controller.common;

import com.wty.shop.common.Const;
import com.wty.shop.pojo.User;
import com.wty.shop.util.CookieUtil;
import com.wty.shop.util.JsonUtil;
import com.wty.shop.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

;

/**
 * @Author wangtengyu
 * @Create 2018-01-30-13:53
 */
public class sessionExpireFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest) request;

        String loginToken= CookieUtil.readLoginToken(httpServletRequest);
        if(StringUtils.isNotEmpty(loginToken)){
            String userJsonStr= RedisShardedPoolUtil.get(loginToken);
            User user= JsonUtil.String2Obj(userJsonStr,User.class);
            if(user!=null) {
                RedisShardedPoolUtil.expire(loginToken, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
            }
        }

        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
